<?php

namespace App\Http\Repositories\Import;

use App\Exceptions\ServiceException;
use App\Http\Constants\CommonExceptionConstants;
use App\Http\Repositories\Eloquent\Car;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class T1 implements ToModel,WithColumnFormatting
{

    public function collection(Collection $rows)
    {

        foreach ($rows as $row)
        {
            print_r($row);exit;
        }
    }

    public function columnFormats(): array
    {
        return [
            'E' => NumberFormat::FORMAT_PERCENTAGE,
            'D' => NumberFormat::FORMAT_DATE_YYYYMMDD2
        ];
    }


    public function model(array $row)
    {
        if (strlen($row[9]) > 15) {

            $dao = new Car();

            if ($dao->plateNo($row[1])) {
                throw new ServiceException(CommonExceptionConstants::getMsg('plate no was repeat'));
            }

            $dao->agent_id = 2;//商户
            $dao->warehouse_id = 1;//仓库
            $dao->brand_id = 1;//品牌
            $dao->car_type_id = 1;//车型
            $dao->color_id = 1;//颜色
            $dao->plate_no = $row[1];//车牌
            $dao->code = $row[8] ? $row[8] : '';//唯一编码
            $dao->vin_code = $row[9] ? $row[9] : '';//VIN码
            $dao->engine_code = $row[10] ? $row[10] : '';//发动机编号
            $dao->mileage_current = 0;
            $dao->mileage_maintain = 0;
            $dao->buy_date = '1971-01-01';//唯一编码
            $dao->inspection_date = '1971-01-01';
            $dao->mark = '';
            $dao->progress = 1;
            $dao->state = 1;

            $dao->save();

        }
    }

}
